package tum0r.utils;

/**
 * 工程: SQLTool<br>
 * 包: tum0r.util<br>
 * 创建者: tum0r<br>
 * 创建时间: 2020/10/3 13:13<br>
 * <br>
 */
public class StringExtension {
    public static boolean isNull(String text) {
        return text == null;
    }

    public static boolean isNotNull(String text) {
        return !isNull(text);
    }

    public static boolean isNullOrEmpty(String text) {
        return isNull(text) || text.length() == 0;
    }

    public static boolean isNotNullOrEmpty(String text) {
        return !isNullOrEmpty(text);
    }

    public static boolean isNullOrOnlyWhiteSpace(String text) {
        return isNull(text) || text.trim().length() == 0;
    }

    public static boolean isNotNullOrOnlyWhiteSpace(String text) {
        return !isNullOrOnlyWhiteSpace(text);
    }

    public static String fillLikeString(String text, boolean left, boolean right) {
        return (left ? "%" : "") + text + (right ? "%" : "");
    }

    public static String fillLikeString(String text) {
        return fillLikeString(text, true, true);
    }

    public static String mask(String text, int start, int stop, int resultLength) {
        String result = null;
        if (isNotNullOrEmpty(text)) {
            stop++;
            String prefix = text.substring(0, start);
            String suffix = text.substring(stop);
            result = prefix + "*".repeat(Math.max(resultLength - prefix.length() - suffix.length(), 0)) + suffix;
        }
        return result;
    }

    public static String mask(String text, int start, int stop) {
        String result = null;
        if (isNotNullOrEmpty(text)) {
            result = mask(text, start, stop, text.length());
        }
        return result;
    }
}
